//
//  WZHeaderView.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/4/20.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZHeaderView.h"
#import "WZHeader.h"

@interface WZHeaderView ()

@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *subTitleLabel;
@property (nonatomic, weak) UIButton *arrowView;
@property (nonatomic, weak) UIView *beginLine;
@property (nonatomic, weak) UIView *endLine;


@end

@implementation WZHeaderView

+ (instancetype)WZHeaderViewWithTableView:(UITableView *)tableView
{
    static NSString *headerID = @"composeApointHeader";
    WZHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    if (header == nil) {
        header = [[WZHeaderView alloc] initWithReuseIdentifier:headerID];
    }
    return header;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap)];
        [self.contentView addGestureRecognizer:tap];
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews
{
    UILabel *title = [[UILabel alloc] init];
    title.textColor = RGBCOLOR(0x92, 0x92, 0x92);
    title.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:title];
    self.titleLabel = title;
    
    
//    UILabel *sub = [[UILabel alloc] init];
//    sub.font = NORFONT16;
//    sub.textColor = TITLECOLOR;
//    sub.textAlignment = NSTextAlignmentRight;
//    sub.numberOfLines = 0;
//    [self.contentView addSubview:sub];
//    self.subTitleLabel = sub;
    
    UIButton *arrow = [UIButton buttonWithType:UIButtonTypeCustom];
    arrow.userInteractionEnabled = NO;
    arrow.imageView.clipsToBounds = NO;
    arrow.imageView.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:arrow];
    self.arrowView = arrow;
    
    
    
    UIView *beginLine = [[UIView alloc] init];
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [self.contentView addSubview:beginLine];
    self.beginLine = beginLine;
    
    UIView *endLine = [[UIView alloc] init];
    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
    [self.contentView addSubview:endLine];
    self.endLine = endLine;
}


- (void)didTap
{
    if ([self.WZHeaderViewDelegate respondsToSelector:@selector(WZHeaderViewDidClick:)]) {
        [self.WZHeaderViewDelegate WZHeaderViewDidClick:self.header];
    }
}

- (void)setHeader:(WZHeader *)header
{
    _header = header;
    self.titleLabel.text = header.headerText;
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willRemoveSubview:newSuperview];
    if (self.header.isOpen) {
        
        self.arrowView.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        return;
    }
    self.arrowView.imageView.transform = CGAffineTransformMakeRotation(0);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.beginLine.frame = CGRectMake(0, 0, self.width, 0.5f);
    self.endLine.frame = CGRectMake(0, self.height - 0.5f, self.width, 0.5f);
    self.titleLabel.frame = CGRectMake(15.0f, 0, 150, self.height - self.beginLine.height - self.endLine.height);
    self.arrowView.frame = CGRectMake(self.width - 15 - 15, 0, 15, self.height - self.beginLine.height - self.endLine.height);
}


@end
